package com.itheima.filter;


import com.alibaba.fastjson.JSON;
import com.itheima.common.R;
import com.itheima.util.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebFilter(filterName = "LoginCheckFilter", urlPatterns = "/*")
//记得在配置类里面扫描过滤器
public class LoginCheckFilter implements Filter {
    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        log.info("拦截到请求：{}", request.getRequestURI());
        //定义不需要处理的请求路径
        String urls[] = new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/user/sendMsg",
                "/user/login",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs",
                "/cache/**"

        };/* "/doc.html", 这四个是swagger放行
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"*/
        boolean check = check(urls, request.getRequestURI());
        //不需要处理直接放行
        if (check) {
            log.info("本次请求{}不需要处理", request.getRequestURI());
            filterChain.doFilter(request, response);//放行
            return;
        }
        //电脑后台需要处理 判断是否已经登录 如果登录则放行
        if (request.getSession().getAttribute("employee") != null) {
            log.info("用户已登录id为：{}", request.getSession().getAttribute("employee"));
            Long empid = (Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empid);//吧id放进线程里去传给MetaObjectHandler自动填充
            filterChain.doFilter(request, response);//放行
            return;
        }
        //手机端需要处理 判断是否已经登录 如果登录则放行
        if (request.getSession().getAttribute("user") != null) {
            log.info("用户已登录id为：{}", request.getSession().getAttribute("user"));
            Long userid = (Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(userid);//吧id放进线程里去传给MetaObjectHandler自动填充
            filterChain.doFilter(request, response);//放行
            return;
        }
        log.info("未登录");
        //未登录 则通过输出流的方式向客户端页面响应数据、再又客户端跳转到登录页面
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }

    //判断放行里的地址和访问的地址是否一致的方法
    public boolean check(String[] urls, String requsetURI) {
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requsetURI);//路径匹配器 一样则返回true
            if (match) {
                return true;
            }
        }
        return false;
    }
}
